// const baseUrl = 'http://beta.tool.77.top/';
const baseUrl = 'https://tool.77.top/';

function request(options) {
  return new Promise((resolve, reject) => {
    // 每次请求时动态获取 token
    const requestHeader = {
      // "content-type": "application/json",
	  'content-type': 'application/x-www-form-urlencoded',
      "token": uni.getStorageSync("token"),
      ...options.header // 允许传入自定义 header
    };
	// if(options.data.entries){
	// for (const [key, value] of options.data.entries()) {
	//   console.log(`${key}: ${value}`);
	// }
	// }
    uni.request({
      url: baseUrl + options.url,
      method: options.method || 'GET',
      data: options.data,
      header: options.header ||requestHeader,
      success: (res) => {
        if (res.data.code === 401) {
			// const pages = getCurrentPages();
			// let cur = pages[pages.length - 1].route
			// console.log(cur);
			// if(cur.indexOf('pages/login') > -1){
			// 	return
			// }
          uni.getSystemInfo({
            success: (systemInfo) => {
              if (systemInfo.uniPlatform === 'devtools') {
                uni.navigateTo({
                  url: '/pages/login/wxlogin'
                });
              }else if(systemInfo.uniPlatform === 'mp-weixin'){
				  uni.navigateTo({
				    url: '/pages/login/wxlogin'
				  });
			  } else {
                uni.navigateTo({
                  url: '/pages/login/h5login'
                });
              }
            },
          });
        } else {
          resolve(res.data);
        }
      },
      fail: (err) => {
        console.error('Request failed:', err);
        reject(err);
      },
    });
  });
}

export default request;